//
//  ReadViewController.m
//  leisure
//
//  Created by xalo on 16/4/18.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "ReadViewController.h"
#import "ReadModel.h"
#import "ReadCollectionViewCell.h"

#import "ReadScrollPictureModel.h"
#import "ReadListViewcontroller.h"


@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)NSMutableArray* modelArr;
@property(nonatomic, strong)UICollectionView* collectionView;
@property(nonatomic, strong)SDCycleScrollView* scrollPicture;
@property(nonatomic, strong)NSMutableArray* pictureArr;

@end

@implementation ReadViewController

-(NSMutableArray *)pictureArr{
    
    if (!_pictureArr) {
        _pictureArr = [NSMutableArray array];
    }
    return _pictureArr;
}

-(NSMutableArray *)modelArr{
    
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}
-(UICollectionView *)collectionView{
    
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(100, 100)];
        [layout setMinimumLineSpacing:10];
        [layout setMinimumInteritemSpacing:10];
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kViewHeight/3 + 64, kViewWidth, kViewHeight*2/3-64) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

-(SDCycleScrollView *)scrollPicture{
    
    if (!_scrollPicture) {
        _scrollPicture = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, kViewWidth, kViewHeight/3) delegate:nil placeholderImage:[UIImage imageNamed:@"placeHolderPicture"]];
    }
    return _scrollPicture;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor greenColor]];
    [self fetchDataSourceWithUrl:kReadURL];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ReadCollectionViewCell"];
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.scrollPicture];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//请求网络数据
-(void)fetchDataSourceWithUrl:(NSString*)URL{
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];//单例初始化网络请求类
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:URL
       parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    }
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
              NSArray *listArr = dic[@"data"][@"list"];
              for (int i = 0; i < listArr.count; i ++) {
                  ReadModel* model = [[ReadModel alloc]init];
                  [model setValuesForKeysWithDictionary:listArr[i]];
                  [self.modelArr addObject:model];
              };
              [self.collectionView reloadData];
              NSArray *pictureArr = dic[@"data"][@"carousel"];
              for (int i = 0; i < pictureArr.count; i++) {
                  ReadScrollPictureModel* model = [[ReadScrollPictureModel alloc] init];
                  [model setValuesForKeysWithDictionary:pictureArr[i]];
                  [self.pictureArr addObject:model];
              }
              self.scrollPicture.imageURLStringsGroup = [self giveUrlArr];
    }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              NSLog(@"0");
    }];
}

-(NSArray*)giveUrlArr{
    NSMutableArray* array = [NSMutableArray array];
    if (self.pictureArr.count!=0) {
        for (int i = 0; i<self.pictureArr.count; i++) {
            ReadScrollPictureModel* model = self.pictureArr[i];
            [array addObject:model.img];
        }
        return array;
    }
    return nil;
}

#pragma mark - UICollectionViewDataSource
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReadCollectionViewCell" forIndexPath:indexPath];
    [cell setCellWithReadModel:self.modelArr[indexPath.row]];
//    cell.corverimgView.image = 
//    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.modelArr.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    

    ReadListViewController* readList = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"ReadListViewController"];
    readList.readModel = [self.modelArr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:readList animated:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
